<template>
  <div class="pagination">
    <button
      :disabled="pageNo === 1"
      @click="$emit('update:pageNo', pageNo - 1)"
    >
      上一页
    </button>
    <!-- 只有连续页的 开始 大于1 的时候 才会展示固定的1 -->
    <button v-if="startEnd.start > 1" @click="$emit('update:pageNo', 1)">
      1
    </button>

    <button v-if="startEnd.start > 2">···</button>
    <!-- 
      v-if和v-for不能一起使用:
        * v-for是列表渲染的指令,v-if是条件渲染的指令
        * v-for的优先级比v-if高(v-for先解析),所以会先执行列表渲染,然后再条件渲染,导致性能降低,并且代码逻辑不够清晰
        * 解决方式:根据列表数据和判断条件,在计算属性中计算出最终的列表渲染条件
     -->
    <button
      v-for="item in pageArr"
      :key="item"
      @click="$emit('update:pageNo', item)"
      :class="{ active: item === pageNo }"
    >
      {{ item }}
    </button>
    <button v-if="startEnd.end < totalPages - 1">···</button>
    <button
      v-if="startEnd.end < totalPages"
      @click="$emit('update:pageNo', totalPages)"
    >
      {{ totalPages }}
    </button>
    <button
      :disabled="pageNo === totalPages"
      @click="$emit('update:pageNo', pageNo + 1)"
    >
      下一页
    </button>
    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  /*
    pageNo:当前页
    total:总条数
    totalPages:总页数
    continue:连续页长度
  */
  props: ["pageNo", "total", "totalPages", "continuePage"],
  computed: {
    startEnd() {
      // console.log(this);
      const { pageNo, totalPages, continuePage } = this;
      let start = null;
      let end = null;

      //先判断连续页的长度是否大于页数,如果大于,则连续的的开始是1 结束是总页数
      if (continuePage > totalPages) {
        start = 1;
        end = totalPages;
      } else {
        //根据当前页计算起始和结尾
        start = pageNo - (continuePage - 1) / 2;
        end = pageNo + (continuePage - 1) / 2;
        //判断临界值,如果起始页小于1
        if (start < 1) {
          start = 1;
          end = continuePage;
        }
        //判断临界值,如果结束页大于总页数
        if (end > totalPages) {
          end = totalPages;
          start = totalPages - continuePage + 1;
        }
      }

      return { start, end };
    },
    pageArr() {
      const arr = [];
      for (var i = 1; i <= this.startEnd.end; i++) {
        if (i >= this.startEnd.start) {
          arr.push(i);
        }
      }
      return arr;
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>